
import {getDepositsList, getDepositById, release, getDepositReleaseList} from '../../services/capital/deposit';
import Constants from '../../utils/Constants';
import {parse} from 'qs';
import { message } from 'antd';
const R = require('ramda');

export default {
    namespace: 'deposit',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        releasePage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },

        list:[],
        releaseList: [],

        dataSource: {},
        deposit: {},
        status: "",
    },
    reducers: {

        //-----------------setList

        /**
         *
         * @param state
         * @param payload
         * @returns {{list, page: *}}
         */
        setlist(state, { payload }) {
            return { ...state, list: payload.list, page: R.isNil(payload.page) ? state.page : payload.page}
        },

        setReleaseList(state, { payload }) {
            return { ...state, releaseList: payload.list, releasePage: R.isNil(payload.page) ? state.releasePage : payload.page}
        },



        //--------------------setEntity


        /**
         * 设置产品
         * @param state
         * @param action
         * @returns {{dataSource: (string|state.dataSource|{}|*)}}
         */
        setDataSource(state, action) {
            let payload = action.payload;
            return {...state, dataSource: payload.data};
        },

    },
    effects: {

        /**
         *
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getDepositsList({payload}, { call, put, select }) {
            let page = yield select(state => state.deposit.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(getDepositsList, {
                status: R.isNil(payload) || R.isNil(payload.status) ? "" : payload.status,
                depositNo: R.isNil(payload) || R.isNil(payload.depositNo) ? "" : payload.depositNo,
                customerno: R.isNil(payload) || R.isNil(payload.customerno) ? "" : payload.customerno,
                beginTime: R.isNil(payload) || R.isNil(payload.beginTime) ? "" : payload.beginTime,
                endTime: R.isNil(payload) || R.isNil(payload.endTime) ? "" : payload.endTime,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setlist',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status: status
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },



        /**
         * 获取保证金
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getDepositById({ payload }, { call, put, select }) {
            const res = yield call(getDepositById, payload);
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                content.forex_relate_id=payload.forex_relate_id;
                yield put({
                    type: 'setDataSource',
                    payload: {
                        data : content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 释放保证金
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *release({ payload }, { call, put, select }) {
            const page = yield select(state => state.deposit.releasePage);
            const res = yield call(release, payload);

            if (res.data.resultCode === "ok") {
                message.info("释放成功！");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 释放保证金列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getDepositReleaseList({payload}, {call, put, select}) {
            const page = yield select(state => state.deposit.releasePage);

            const res = yield call(getDepositReleaseList, {id: payload.id, page});

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setReleaseList',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },



    },
    subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};